import { _decorator, Component, Node, Vec3, RigidBody, BoxColliderComponent } from 'cc';
import { poolManager } from '../../../../Framework/Scripts/Managers/poolManager';
import myUtil from '../../../../Framework/Scripts/Utils/myUtil';
import { Vector3 } from '../../../../Framework/Scripts/Utils/Vector3';
import { constant } from '../../constant';
import { GameApp } from '../../GameApp';
import { enemyBase } from '../enemyBase';
import MapNavAgent from '../MapNavAgent';
import { playerSkillCollider } from '../playerSkillCollider';
const { ccclass, property } = _decorator;

@ccclass('tornado')
export class tornado extends Component {

    private maxR: number = 3//半径最大
    private oldMaxR: number = 3
    private startPos: Vec3 = new Vec3()
    private navTs: MapNavAgent = null
    private count: number = 0
    private maxCount: number = 10
    private oldMaxCount: number = 10

    private oldAp: number = 3

    private oldScale: number = 1

    private body: playerSkillCollider = null


    start() {

    }

    onLoad() {
        this.body = this.node.getChildByName("body").getComponent(playerSkillCollider)
        this.oldAp = this.body.AP
        this.navTs = this.node.getComponent(MapNavAgent)


    }
    public moveToPos(endPos: Vec3) {
        
        const scale: number = this.oldScale + GameApp.Instance.playerData.config.tornado_scale.level * 0.1
        this.body.node.setScale(new Vec3(scale, scale, scale))
        this.body.AP = this.oldAp + GameApp.Instance.playerData.config.tornado_attack.level * 0.3

        this.maxCount = this.oldMaxCount + GameApp.Instance.playerData.config.tornado_time.level * 1

        this.maxR = this.oldMaxR + GameApp.Instance.playerData.config.tornado_Range.level * 0.3

        this.navTs.InitMapNavAgent(20)
        this.navTs.NavOnRoad([endPos], () => {
            this.navTs.InitMapNavAgent(8)
            this.startPos = Vector3.add(Vector3.mul(this.node.forward, this.maxR), endPos)
            this.move()

        })
    }
    private move() {

        const d: number = myUtil.Random(0, 360)
        //   console.log("角度：", d);

        const x: number = Math.cos(d)
        const y: number = Math.sin(d)
        const r: number = myUtil.Random(1, this.maxR)
        //  console.log("半径", r);

        const endPos: Vec3 = Vector3.add(new Vec3(x * r, this.startPos.y, y * r), this.startPos)

        this.navTs.NavOnRoad([endPos], () => {
            this.count++
            //    console.log("count:", this.count);

            if (this.count <= this.maxCount) {
                this.move()
            } else {
                this.count = 0
                this.navTs.StopNav()

                poolManager.instance.putNode(this.node)
            }


        })
    }
    update(deltaTime: number) {

    }
}


